package com.sxit.tdf.server.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaMode;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sxit.tdf.server.entity.Professional;
import com.sxit.tdf.server.entity.bo.ProfessionalInfoBo;
import com.sxit.tdf.server.entity.bo.SimpleProfessionalBo;
import com.sxit.tdf.server.entity.vo.DeleteProfessionalVo;
import com.sxit.tdf.server.entity.vo.InsertProfessionalVo;
import com.sxit.tdf.server.entity.vo.SiftProfessionalInfoVo;
import com.sxit.tdf.server.response.R;
import com.sxit.tdf.server.service.IProfessionalService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 专业 前端控制器
 *
 * @author 橙子高
 * @since 2022-02-08
 */
@RestController
@RequestMapping("/professional")
@Tag(name = "ProfessionalController", description = "专业 前端控制器")
public class ProfessionalController {

  @Autowired private IProfessionalService professionalService;

  @GetMapping("/getSimpleProfessional")
  @Operation(summary = "获取简易的专业列表")
  @SaCheckLogin
  public R getSimpleProfessional() {
    List<SimpleProfessionalBo> simpleProfessionalList =
        professionalService.getSimpleProfessionalList();
    return R.ok().put("data", simpleProfessionalList);
  }

  @Operation(summary = "获取专业的信息")
  @PostMapping("/getProfessionalList")
  @SaCheckLogin
  @SaCheckPermission("PROFESSIONAL:SELECT")
  public R getProfessionalList(@Valid @RequestBody SiftProfessionalInfoVo professionalInfoVo) {

    IPage<ProfessionalInfoBo> professionalList =
        professionalService.getProfessionalList(professionalInfoVo);

    long total = professionalList.getTotal();
    List<ProfessionalInfoBo> records = professionalList.getRecords();
    return R.ok().put("total", total).put("data", records);
  }

  @Operation(summary = "添加专业信息")
  @PostMapping("/insertProfessional")
  @SaCheckPermission(
      value = {"PROFESSIONAL:UPDATE", "PROFESSIONAL:INSERT"},
      mode = SaMode.OR)
  public R insertProfessional(@Valid @RequestBody InsertProfessionalVo insertProfessionalVo) {

    Professional professional = new Professional();

    if (insertProfessionalVo.getId() == null) {
      professional.setProfessionalName(insertProfessionalVo.getProfessionalName());
      professional.setProfessionalCode(insertProfessionalVo.getProfessionalCode());
      professional.setProfessionalDesc(insertProfessionalVo.getProfessionalDesc());
      professional.setDeptId(insertProfessionalVo.getDeptId());

      boolean save = professionalService.save(professional);

      if (save) {
        return R.ok("保存成功");
      } else {
        return R.error("保存失败");
      }
    } else {
      professional.setId(insertProfessionalVo.getId());
      professional.setProfessionalName(insertProfessionalVo.getProfessionalName());
      professional.setProfessionalCode(insertProfessionalVo.getProfessionalCode());
      professional.setProfessionalDesc(insertProfessionalVo.getProfessionalDesc());
      professional.setDeptId(insertProfessionalVo.getDeptId());

      boolean save = professionalService.updateById(professional);

      if (save) {
        return R.ok("修改成功");
      } else {
        return R.error("修改失败");
      }
    }
  }

  @Operation(summary = "通过Id删除专业信息")
  @DeleteMapping("/delete")
  @SaCheckLogin
  @SaCheckPermission("PROFESSIONAL:DELETE")
  public R delete(@Valid @RequestBody DeleteProfessionalVo deleteProfessionalVo) {

    Boolean delete = professionalService.deleteByid(deleteProfessionalVo.getId());

    if (delete) {
      return R.ok("删除成功");
    } else {
      return R.ok("删除失败");
    }
  }

  @Operation(summary = "获取指定部门下的专业")
  @GetMapping("/getProfessionalByDeptId")
  @SaCheckLogin
  public R getProfessionalByDeptId() {
    List<SimpleProfessionalBo> list = professionalService.getProfessionalByDeptId();
    return R.ok().put("data", list);
  }
}
